Error making POST from a json to a Web API

Asked

Viewed 108 times

0

It’s giving me an error when I try to make a POST to the API that error is:

System.Net.Webexception: Remote server returned an error: (405) Method not allowed.

The code I’m using is:

var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://test-turnoverapi.lyoness.net/");


httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/json";
httpWebRequest.Accept = "application/vnd.lyoness.servicesv1 + json";
httpWebRequest.Date = d2;
httpWebRequest.Headers.Add("Authorization", ": " + hashheader);


using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    MessageBox.Show("Entrei");
    streamWriter.Write(json);
    streamWriter.Flush();
    streamWriter.Close();
}

MessageBox.Show(json);
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
    MessageBox.Show("Entrei");
    var teste = streamReader.ReadToEnd();
    MessageBox.Show(teste);
}

I would like to know what is the problem with my code and how I can solve it.

Here is an example of a json that the Api provides :

Segue-se um exemplo de um json que a Api fornece

  • Does the API have a documentation page? you can post?

  • @Thomaserichpimentel can only post an example of a json they have made either ?

  • The more information, the better! but it would be interesting some documentation.. which is the API website?

  • @Thomaserichpimentel already added a print with an example they provide

  • @Perozzo It’s not the same question

  • Maybe it is, since you want to use a REST service in the wrong way. Read the link I put in and check what you’re doing wrong. The recommended object to be used for this type of request is an Httpclient.

  • HttpClient is equivalent to WebRequest

  • It is equivalent, but whenever I read about it (courses, tutorials, etc.), I recommend using Httpclient and not Webrequest.

  • @LINQ But this so far was the only way that resulted something all the other forms I tested showed no results

  • Pedro, the problem is the URL you are making the request.

  • @LINQ will test another URL that is provided to me then.

  • Your API does not accept post or dua url is wrong. You can use Postmam to better test your API first.

  • @LINQ was that very thank you

Show 8 more comments
No answers

Browser other questions tagged

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