简体   繁体   中英

HttpClient and REST (Getflix API)

I'm stumped here and hoping for some help. I'll paste in the relevant section of the Getflix API ( https://getflix.zendesk.com/hc/en-gb/articles/201689644-API-Resource-Regions ):


POST v1/regions.json Updates the region for a specified service. Only 1 service can be updated at a time, and the following JSON object must be present in the POST body.

{"service": < serviceName >,"region": < regionCode >}

serviceName: is a valid service name (see region list below) regionCode: is the ISO 2 letter country code for the region to switch to (in upper-case).

Example Usage

curl -u <api_key>:x -X POST -d '{"service":"netflix","region":"US"}' https://www.getflix.com.au/api/v1/regions.json

And here is my code:

string response = await theHandler.POSTreq("https://www.getflix.com.au/api/v1/regions/list.json", "{\"service\":\"netflix\",\"region\":\"US\"}");

and here is theHandler.POSTreq:

public async Task<string> POSTreq(string requestURL, string sendJson)
    {

        //set credentials
        HttpClientHandler handler = new HttpClientHandler();
        handler.Credentials = new NetworkCredential(apiKey, "x");

        Uri requestUri = new Uri(requestURL);


        var objClint = new HttpClient(handler);
        HttpResponseMessage respon = await objClint.PostAsync(requestUri, new StringContent(sendJson, System.Text.Encoding.UTF8, "application/json"));

        string responJsonText = await respon.Content.ReadAsStringAsync();
        return responJsonText;
    }

As far as I can figure out, this SHOULD work. it doesn't. The response text I end up getting isn't an error but a whole bunch of HTML.

Anyone know what I'm doing wrong? I've spent hours on this and I'm stumped. ha

Yes, I'm an idiot. I was using the wrong URL. lol.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM