I have this LoansController
for a web api
[Route("api/[controller]")]
[ApiController]
public class LoansController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}
// POST api/loans
[HttpPost]
public void Post([FromBody] string value)
{
}
}
In PowerShell I can call
Invoke-WebRequest http://localhost:1113/api/loans -Body $postParams -Method Get
and it works fine (I get value1
and value2
)
But when I try
$postParams = "{'value':'123'}"
Invoke-WebRequest http://localhost:1113/api/loans -Body $postParams -Method Post # -ContentType 'Application/json'
I just keep getting
Invoke-WebRequest : The remote server returned an error: (400) Bad Request.
What am I doing wrong?
I tried adding -ContentType 'Application/json'
but it made no difference
What am I missing?
I also tried Invoke-RestMethod
but with the same results..
Next I removed [FromBody]
from the value
param but value
now comes in as null
Incorrect file size The most common case is: you upload a file to a website, the upload takes quite a long time, and finally, you receive the error “HTTP Error 400 Bad Request”. Simply, the file is too large and hits the file size limit on the website's server, so the server responds with the error code.
Check for Invalid HTTP Headers It's possible that the 400 error you're seeing from your own application is a result of missing or invalid custom HTTP headers. In such cases, you may be able to analyze the HTTP headers that are sent on the server-side and determine if they are invalid or unexpected in some way.
This issues is usually caused by a corrupted cookie that is too long. Clear the Cache and remove the Cookies for websites that cause problems via the "3-bar" Firefox menu button (Options/Preferences). If clearing cookies didn't help then it is possible that the cookies.
Reason
It just happens because your action method is expecting a plain string
from HTTP request's Body:
[HttpPost]
public void Post([FromBody] string value)
{
}
Here a plain string is a sequence of characters which is quoted by ""
. In other words, to represent the string, you need include the quotes before and after these characters when sending request to this action method.
If you do want to send the json string {'value':'123'}
to server, you should use the following payload :
POST http://localhost:1113/api/loans HTTP/1.1
Content-Type: application/json
"{'value':'123'}"
Note : We have to use doublequoted string ! Don't send string without the ""
How to fix
To send a plain string, simply use the following PowerShell
scripts :
$postParams = "{'value':'123'}"
$postParams = '"'+$postParams +'"'
Invoke-WebRequest http://localhost:1113/api/loans -Body $postParams -Method Post -ContentType 'application/json'
Or if you would like to send the payload with json, you could create a DTO
to hold the value
property:
public class Dto{
public string Value {get;set;}
}
and change your action method to be :
[HttpPost]
public void Post(Dto dto)
{
var value=dto.Value;
}
Finally, you can invoke the following PowerShell
scripts to send request :
$postParams = '{"value":"123"}'
Invoke-WebRequest http://localhost:1113/api/loans -Body $postParams -Method Post -ContentType 'application/json'
These two approaches both work flawlessly for me.
Try to add headers
$url = 'http://localhost:1113/api/loans'
$head = @{'ContentType'='Application/json'}
Invoke-WebRequest -Uri $url -Body $postParams -Method Post -Headers $head
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With