As you probably learned, you cannot. When you call PostAsJsonAsync , the code converts the parameter to JSON and sends it to the request body. Your parameter is a JSON array that will look something like this:
[1,"/","?aid",345,"&content=","aGVsbG8gd29ybGQ="]
This is not what the first function expects (at least, what I imagine, since you did not provide route information). There are several issues here:
- By default, parameters of type
byte[] (reference types) are passed in the request body, and not in the URI (unless you explicitly mark the parameter with the [FromUri] attribute). - Other parameters (again, based on my assumption about your route) should be part of the URI, not the body.
The code looks something like this:
var uri = "api/Test/" + id + "/?aid=" + aid; using (HttpResponseMessage response = client.PutAsJsonAsync(uri, filecontent).Result) { return response.EnsureSuccessStatusCode(); }
Now there is another potential problem with the code above. It waits for a network response (what happens when accessing the .Result property in the Task<HttpResponseMessage> returned by PostAsJsonAsync . Depending on the environment, it can worse happen that it can be deadlocked (waiting in the thread for a network response to respond). In the best case, this the thread will be blocked for the duration of the network call, which is also bad. Consider using asynchronous mode (waiting for the result, returning the Task<T> in your action) instead, as in the example below
public async Task<HttpResponseMessage> Put(int id, int aid, byte[] filecontent) {
Or without the async / wait keywords:
public Task<HttpResponseMessage> Put(int id, int aid, byte[] filecontent) {
source share