Can´t make proper request Rest Template

coder Tester :

So i am calling an API(This API is used to send SMS's) in my Java project using a GET.

This is the API URL:

http://xxxxx.smsapi.xx/xxxx/xxxx.aspx?user=username&password=password&msisdn=xxxxxx&sid=1111111&msg=test%20msg&fl=0

and when i make the call in my broweser i get a good response like this:

{"ErrorCode":"000",
"ErrorMessage":"Success",
"JobId":"id",
"MessageData":
[{"Number":"xxxxxxx",
"MessageParts":[{"MsgId":"id",
"PartId":1,
"Text":"test msg"}]
}]}

But when i make the Call in my project i get the same exact message but the text field comes like this:

"Text":"test%20msg"

and the text sended to the phone is sended with the "%20", and that is not what i want, the response is comming wrong.

this is my java code at the moment:

    UriComponentsBuilder builder = UriComponentsBuilder
         .fromUriString(env.getProperty("smsProviderUrl"))
         // Add query parameter     
         .queryParam("user", env.getProperty("smsUser"))
         .queryParam("password", env.getProperty("smsPassword"))
         .queryParam("msisdn", new Object[] {number})
         .queryParam("sid", sendedID)
         .queryParam("msg", body)                           
         .queryParam("fl", env.getProperty("smsFl"));

    RestTemplate template = this.restTemplate();

    List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();        

    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

    converter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_HTML));   

    messageConverters.add(converter);  

    template.setMessageConverters(messageConverters);


    SmsResponseDTO response = template.getForObject(builder.toUriString(), SmsResponseDTO.class);

this are the headers sent on the browser:

Content-Type: text/html; charset=utf-8

And after using wireshark to get the packets i found this is the header sent from Java

Accept: text/html\r\n

I already tried to add the UTF-8 charset but it didnt work, and i think that utf-8 is set by default so i am not sure if that is the problem, i think it has something to do with headers im not sure.

Thank you!

Karol Dowbecki :

You are most likely encoding the request URI twice. Start by confirming that body doesn't contain %20 already, make sure that this is plain text message.

Then manually convert UriComponentsBuilder to String with:

String uri = UriComponentsBuilder
     .fromUriString(env.getProperty("smsProviderUrl"))
     // Add query parameter     
     .queryParam("user", env.getProperty("smsUser"))
     .queryParam("password", env.getProperty("smsPassword"))
     .queryParam("msisdn", new Object[] {number})
     .queryParam("sid", sendedID)
     .queryParam("msg", body)                           
     .queryParam("fl", env.getProperty("smsFl"))
     .encode()
     .build()
     .toUriString();

and confirm that URI contains &msg=test%20msg and not &msg=test%2520msg. Do notice that double encoding will change % to %25.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=101483&siteId=1