'REST API call works at Postman but not at Jenkins

I'm facing a problem that seems to be simple. I'm trying to call a REST API from Jenkins build step and it's not working. I make this call from postman and it works just fine, but from Jenkins, it doesn't. Bellow are some pictures of my configurations.

Postman Settings

As you can see on this image, at postman my request works perfectly.

Jenkins Settings

At this image you can see that I'm using the same settings at Jenkins, although it doesn't work.

Finally, the error message that is presented when I try to run the Jenkins.

Started by user Hugo da Silva da Silva
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/Power BI - Atualizador mensal
HttpMethod: POST
URL: https://login.windows.net/7348b8c4-3705-4965-b18a-c02e2d5a28eb/oauth2/token
Content-Type: application/x-www-form-urlencoded; charset=ISO-8859-1
Accept: application/json
Sending request to url: https://login.windows.net/7348b8c4-3705-4965-b18a-c02e2d5a28eb/oauth2/token
Response Code: HTTP/1.1 400 Bad Request
ERROR: Build step failed with exception
hudson.AbortException: Fail: the returned code 400 is not in the accepted range: [[100‥399]]
    at jenkins.plugins.http_request.HttpRequestExecution.responseCodeIsValid(HttpRequestExecution.java:369)
    at jenkins.plugins.http_request.HttpRequestExecution.processResponse(HttpRequestExecution.java:379)
    at jenkins.plugins.http_request.HttpRequestExecution.authAndRequest(HttpRequestExecution.java:295)
    at jenkins.plugins.http_request.HttpRequestExecution.call(HttpRequestExecution.java:226)
Caused: java.lang.IllegalStateException
    at jenkins.plugins.http_request.HttpRequestExecution.call(HttpRequestExecution.java:229)
    at jenkins.plugins.http_request.HttpRequestExecution.call(HttpRequestExecution.java:79)
    at hudson.remoting.LocalChannel.call(LocalChannel.java:45)
    at jenkins.plugins.http_request.HttpRequest.perform(HttpRequest.java:398)
    at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
    at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:741)
    at hudson.model.Build$BuildExecution.build(Build.java:206)
    at hudson.model.Build$BuildExecution.doRun(Build.java:163)
    at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:504)
    at hudson.model.Run.execute(Run.java:1856)
    at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
    at hudson.model.ResourceController.execute(ResourceController.java:97)
    at hudson.model.Executor.run(Executor.java:428)
Build step 'HTTP Request' marked build as failure
Finished: FAILURE

Anybody has ever faced a similar situation? Any tip?

Best Regards



Solution 1:[1]

Posting here the solution given by my friend @brunolmfg.

The problem was the body content format. I was posting it using json format, whereas I shoud post it as url encoded. Here is the body content that worked:

grant_type=client_credentials&resouce=XXX&client_id=YYY&client_secret=ZZZZ

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 Hugo da Silva da Silva