Web API Consumption

hversemann

Member
Joined
Jul 29, 2016
Messages
11
Location
midwest
Programming Experience
3-5
I'm building a new web application tool that is supposed to send requests to an API and then bring the responses in JSON format.

I believe I'm formatting the request properly, but I keep getting 404 responses like this:

StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Pragma: no-cache
Status: 404 Not Found
X-Canvas-Meta: q=1439;b=1168336;m=1168336;u=0.01;y=0.00;d=0.02;a=1;g=3H40hCeLpWpYU3TZwdT7ZhvrXSzOThpcEQF0SnIx;s=1689;c=cluster30;z=us-east-1b;
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Request-Context-Id: 99a4fb51-88ef-4479-a5ef-a8b4fc807bbe
X-Request-Processor: 097790bc5a35e7ba0
X-Runtime: 0.080932
X-Session-Id: caa61c18d60a07f93162e461f4ef3ecd
X-UA-Compatible: IE=Edge,chrome=1
X-XSS-Protection: 1; mode=block
Connection: keep-alive
Cache-Control: no-store, must-revalidate, no-cache, max-age=0
Date: Mon, 24 Jul 2017 19:14:00 GMT
P3P: CP="None, see .......
Server: Apache
Content-Length: 17755
Content-Type: text/html; charset=utf-8
}

and so far have not been able to figure this out.
my original GET request looks like this:

http://xxxxxxxxxxxxxxxxx.instructure.com/v1/accounts/{account id}/terms?access_token={token value}

To me I think the way the request above seems to be formatted seems to be right, and does appear to match a valid request on the api.

I'm still looking but haven't found anything helpful yet on this, so I would appreciate any help offered.

Thanks.

Henry
 

hversemann

Member
Joined
Jul 29, 2016
Messages
11
Location
midwest
Programming Experience
3-5
I think the problem I'm having is because I'm not sending the "content-type" header, and its defaulting to "text/html; charset=utf-8", when it should be set to "application/json; utf-8".
I'm trying to use the HttpClient with the GetStringAsync(<url here>) method, but I haven't found any good complete or working example yet of how to set the content-type header for the response that I expect back.

Would appreciate any help in finding a working example that I can use with C# (2013), Visual Studio Ultimate 2013, ASP.Net Web Frameworks and Tools 2012.2 (4.1.21001.0), and ASP.Net Web Frameworks and Tools 2013 (5.0.11001.0) as I've had no luck so far trying different examples which I have found and am continuing to look for.

Thanks for the help.

Henry
 

Brent

New member
Joined
May 26, 2011
Messages
2
Location
Meadville, PA
Programming Experience
10+
I believe DefaultRequestHeaders is what you are looking for...
csharp:
private static HttpClient Client;
public async Task<MyObj> PostData()
{
    Client = new HttpClient(new HttpClientHandler() { UseDefaultCredentials = true });
    // Clear headers and add a new MediaTypeWithQualityHeaderValue
    Client.DefaultRequestHeaders.Accept.Clear();
    Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
    //...
}
Hope this points you in the right direction!
 
Top Bottom