I am pretty new to APIs and am struggling with POST requests. Specifically, I need help putting my headers in the right places. I’ve been unable to use fiddler to look at my request because I am running it through an android emulator and it gets complicated, but I have been able to extract the request from debugging the code. I am getting a bad request error right now, but I am also constantly being told I am using the wrong header in the wrong place when I try to experiment.
try
{
string apiPath = ApiPath.Trim();
Dictionary<string, string> headers = new Dictionary<string, string>();
//headers.Add("content-type", "application/json; odata=verbos");
//headers.Add("content-type", "application/json");
headers.Add("Accept-Encoding", "gzip, deflate, br");
headers.Add("Accept", "*/*");
StringContent stringContent = new StringContent(JsonConvert.SerializeObject(parameters), Encoding.UTF8, "application/json");
stringContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
//HttpContent content = new HttpContent;
//Dictionary<string, string> headers = new Dictionary<string, string>();
HttpResponseMessage token = await client.InvokeApiAsync(apiPath, stringContent, HttpMethod.Post,headers,null );
catch (MobileServiceInvalidOperationException ex)
{
string RequestDump = ex.Request.ToString();
string RequestDump2 = ex.Request.RequestUri.ToString();
string RequestDump3 = ex.Request.Headers.ToString();
string RequestDump4 = ex.Request.Content.Headers.ToString();
return null;
}
RequestDump
"Method: POST, RequestUri: '$myazureurl$/api/v1/Meal/9077dfbf-7565-403c-b390-801ed61650bf', Version: 2.0, Content: System.Net.Http.StringContent, Headers:rn{rn Accept-Encoding: gziprn Accept-Encoding: deflatern Accept-Encoding: brrn Accept: */*rn X-ZUMO-FEATURES: AGrn X-ZUMO-INSTALLATION-ID: $InstallID$rn User-Agent: ZUMO/4.2rn User-Agent: (lang=Managed; os=Android; os_version=9; arch=Unix; version=4.2.0.0)rn X-ZUMO-VERSION: ZUMO/4.2 (lang=Managed; os=Android; os_version=9; arch=Unix; version=4.2.0.0)rn ZUMO-API-VERSION: 2.0.0rn Content-Type: application/jsonrn Content-Length: 133rn}"
RequestDump2
"$myazureurl$/api/v1/Meal/9077dfbf-7565-403c-b390-801ed61650bf"
RequestDump3
Accept-Encoding: gzip, deflate, brrnAccept: */*rnX-ZUMO-FEATURES: AGrnX-ZUMO-INSTALLATION-ID: $InstallationID$rnUser-Agent: ZUMO/4.2 (lang=Managed; os=Android; os_version=9; arch=Unix; version=4.2.0.0)rnX-ZUMO-VERSION: ZUMO/4.2 (lang=Managed; os=Android; os_version=9; arch=Unix; version=4.2.0.0)rnZUMO-API-VERSION: 2.0.0rn
RequestDump4
Content-Type: application/jsonrnContent-Length: 133rn
Here is what I got from fiddler when executing the same post on my API while running on my local.
POST https://localhost:44362/api/v1/Meal/F1A3EF0E-EBA0-437E-9E4B-4851074500EE HTTP/1.1
Host: localhost:44362
Connection: keep-alive
Content-Length: 116
accept: */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36
Content-Type: application/json
Origin: https://localhost:44362
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: https://localhost:44362/swagger/index.html
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
{"userGUID":"F1A3EF0E-EBA0-437E-9E4B-4851074500EE","Name":"fffser","Description":"rrer","madeItBefore":true}
I am tearing my hair out. Not sure what goes where. Also, is the "entity" object a different name for the "content" object?
Source: Android Questions