'Making a request to a third-party API return 400 error in ASP.Net Core
I have the below code that makes a get request to a payment integrator. The API works of the integrator work totally fine when making the request from postman. The code
try
{
string data = "entityId=xx";
string url = "https://xy/checkouts/" + CheckoutId + "/payment?" + data;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "GET";
request.Headers["Authorization"] = "Bearer AAA";
string ResDescription, ResCode;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string webResponseAsString = reader.ReadToEnd();
dynamic dynamicResult = JToken.Parse(webResponseAsString);
ResDescription = dynamicResult.result.description;
ResCode = dynamicResult.result.code;
reader.Close();
dataStream.Close();
}
if (ResCode == "000.000.000" || ResCode == "000.000.100" || ResCode == "000.100.112")
{
await EnrollUser(UserId, CourseId, CopounId, OrderId);
return Ok(ResDescription);
}
else
{
return BadRequest(ResDescription);
}
}
catch (Exception x)
{
return BadRequest(x);
}
however when I run the code I get this error:
{"ClassName":"System.Net.WebException","Message":"The remote server returned an error: (400) .","Data":null,"InnerException":null,"HelpURL":null,"StackTraceString":" at System.Net.HttpWebRequest.GetResponse()\r\n at IoT_Kids.Controllers.Payments.SwitchController.CompletePayment(String CheckoutId, String UserId, String OrderId, Int32 CourseId, Int32 CopounId) in C:\Projects\IoT Projects\IoT Project\Controllers\Payments\SwitchController.cs:line 310","RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":null,"HResult":-2146233079,"Source":"System.Net.Requests","WatsonBuckets":null}
It is surprising for me to show a point to the local path of the project even though the project is running on the cloud. Why is that? If anyone could help with what is wrong with my code will be great. Thank you
Solution 1:[1]
in postman you can click 'code' (or the </> icon) and choose c# to see the difference between your request and the one in postman.
The 400 error means the request is bad, and based on the message, Data is null. I think it's expecting you to post this data in as a body and not a get url param.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | terrencep |
