简体   繁体   中英

Why does postman substitute characters in my get request string?

I am making a get request through postman see image from postman

and when the request reaches my controller, the characters like "+" are converted to empty spaces " ". The api is written in c#.

My controller code is:

[RoutePrefix("")]
public partial class _DesifrarCusController : CaramlController, I_DesifrarCusController 
{
    [HttpGet]
    [Route("descifrarCus")]
    public IHttpActionResult GetDescifrarCusResponse( [FromUri] string cus = null ) {
        return Ok (ExecGetDescifrarCusResponse(cus));
        //return Ok();
    }
}

The debugging of my code can be seen in the following screenshot: debugging postman request

Does anyone have any idea what postman is doing with the request string?

I have tried to send the string between quotes and with ascii characters and still the problem persists.

I add capture of the postman headers

You need to encode the parameter on Postman. First add the parameter under Params tab

在此处输入图像描述

then select the value and right click the value chose EncodeURIComponent 在此处输入图像描述

then you will get encoded parameter

在此处输入图像描述

Reference: https://learning.postman.com/docs/sending-requests/requests/

Postman send data from query by encoding it with urlencode.This is normal. Sending this kind of long data from url is not best practise.I recommend use HttpPost and send data from body.

Data in URIs need to be UrlEncoded . When your C# application recevies the data it will decode it which will replace the plus signs with spaces (which to be fair is the older standard).

Postman won't do this automaticly for you, but it can help.

If you have a value like this.

邮递员中非编码网址的图片

You can select the text and right click it to get the option to encode the value.

Postman 中用于编码值的上下文菜单的图片

This will encode the value properly and it will be received in your C# application as expected.

邮递员中编码网址的图片

As people mentioned in other answers, it's encoding issue. Alternatively you could use [FromBody] and then just put your string in body request part of postman

public IHttpActionResult GetDescifrarCusResponse( [FromBody] string cus = null ) {
        return Ok (ExecGetDescifrarCusResponse(cus));
        //return Ok();
    }

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