Post with JSON - Httpclient C#

Asked

Viewed 569 times

1

I need to do a Postasync in my web-api, passing a JSON as parameter. I tried it as follows, but I have not had success so far... Regardless of the structure of this JSON, I get a Bad Request when executing the post.

Can anyone tell me what’s missing from this code?

var url = "URL";

http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));

JArray array = new JArray();
JValue title = new JValue(drive.result.NAME);
JValue fileContent = new JValue(base64);
JValue entityId = new JValue(idNegocio);

array.Add(title);
array.Add(fileContent);
array.Add(entityId);

string JSON = array.ToString();

var parametro = new StringContent(JSON.ToString(), Encoding.UTF8, "application/json");

var post = await http.PostAsJsonAsync(url, parametro);

if (post.IsSuccessStatusCode)
{
    return "Sucesso!";
}
else
{
    return post.Content.ToString();
}

JSON has to have a structure similar to :

{
    "fields": {
        "title": "a",
        "fileContent": "b",
        "entityId": "c"
    }
}
  • Take a look at this article: http://www.macoratti.net/16/11/c_webreq1.htm

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.