Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(400) Bad Request when trying to post simple value to an API

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

like image 622
Bassie Avatar asked Dec 08 '18 15:12

Bassie


People also ask

Why do I keep getting HTTP 400 Bad Request?

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.

How do I fix 400 Bad Request postman?

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.

How do I fix this HTTP error 400 the size of the request headers is too long?

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.


2 Answers

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

  1. 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'
    
  2. 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.

like image 85
itminus Avatar answered Nov 04 '22 23:11

itminus


Try to add headers

$url = 'http://localhost:1113/api/loans'
$head = @{'ContentType'='Application/json'}
Invoke-WebRequest -Uri $url -Body $postParams -Method Post -Headers $head 
like image 40
Mike Twc Avatar answered Nov 04 '22 23:11

Mike Twc