简体   繁体   English

JAVA-处理针对Content-Type的HttpClient请求:application / json

[英]JAVA - Process HttpClient request for Content-Type: application/json

Hope everyone is doing great. 希望每个人都做得很好。

problem : I am getting the following response every time I make a call to api for login via POST request 问题 :每次通过POST请求调用api进行登录时,都会得到以下响应

[status] => 415
[error] => Unsupported Media Type
[message] => Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

Here is my code for Yii2 application Side : 这是我的Yii2应用程序端代码:

$url = ['auth/login'];
$data = ['email'=>$email, 'password'=>$password];

$response = \Yii::$app->apiclient->createRequest()
    ->setMethod('post')

    ->setUrl($url)

    ->addHeaders(['Accept-Charset' => 'UTF-8'])
    ->addHeaders(['Accept' => 'application/json'])
    ->addHeaders(['Content-Type' => 'application/json'])
    ->addHeaders(['Accept-Language' => \app\helpers\Helper::getUserLanguage()])

    ->setData($data)

    ->send();

return $response;

Here is what my apiclient setting is in web.conf file under components section 这是我的apiclient设置在组件部分下的web.conf文件中

'apiclient' => [
            'class' => 'yii\httpclient\Client',
            'baseUrl' => 'http://myserverName:8000',
        ],

Here is my code for Spring (JAVA) API side : 这是我的Spring(JAVA)API端代码:

@RequestMapping(value = "/auth/login", method = RequestMethod.POST)
public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest) {}

Target : I want to get user data from api on if login credentials (email & password) are correct. 目标 :如果登录凭据(电子邮件和密码)正确,我想从api获取用户数据。 Or even a validation error message will do 甚至会出现验证错误消息

Context : I am using Yii2 framework as my Application (front-end) and Spring (JAVA) as my back-end (API) 上下文 :我正在使用Yii2框架作为我的应用程序(前端),并使用Spring(JAVA)作为我的后端(API)

side-note : the fun fact is that I am not even trying for content type application/x-www-form-urlencode as you can see in my Yii2 code but still I am getting the response mentioned in the problem. 旁注 :有趣的事实是,我什至没有尝试使用内容类型application/x-www-form-urlencode正如您在Yii2代码中看到的那样,但是我仍然得到问题中提到的响应。

Another fun fact is that when I call out for the same API in postman, I dont get any error and my response is exactly as it should be. 另一个有趣的事实是,当我在邮递员中调用相同的API时,我没有收到任何错误,并且我的响应完全正确。

Any help is appreciated. 任何帮助表示赞赏。

Add this to your request: 将此添加到您的请求:

->setFormat(\\yii\\httpclient\\Client::FORMAT_JSON) -> setFormat(\\ yii \\ httpclient \\ Client :: FORMAT_JSON)

Try this : 尝试这个 :

@RequestMapping(value = "/auth/login", method = RequestMethod.POST, consumes = "application/json")
public ResponseEntity<?> authenticateUser(@Valid @RequestBody LoginRequest loginRequest, BindingResult bindingResult) {}

This may help you.. 这可能对您有帮助。

Thanks :) 谢谢 :)

The issue was wit the request I was sending to api. 问题出在我发送给api的请求上。

Here is what I needed to do 这是我需要做的

$response = \Yii::$app->apiclient->createRequest()
            ->setMethod('POST')
            ->setUrl($url)
            ->setData($data)
            ->setFormat(\yii\httpclient\Client::FORMAT_JSON)

            ->addHeaders(['accept-charset' => 'utf-8'])
            ->addHeaders(['accept' => 'application/json'])
            ->addHeaders(['content-type' => 'application/json'])
            ->addHeaders(['accept-language' => \app\helpers\Helper::getUserLanguage()])

            ->send();

I added following line and it worked 我添加了以下行,它的工作原理

->setFormat(\yii\httpclient\Client::FORMAT_JSON)

Note : 注意事项

I have no idea about how and why it worked but this might be because PHP is a loosely typed language and JAVA is opposite. 我不知道它如何工作以及为什么起作用,但这可能是因为PHP是一种松散类型的语言,而JAVA是相反的。 Java would ask for all minor details to be sent in order to process a request. Java会要求发送所有次要细节以处理请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java HttpClient改变内容类型? - Java HttpClient changing content-type? 没有为Content-Type定义解析器:application / json - No parser defined for Content-Type: application/json 在 Spring 中读取 Content-Type 应用程序/json - Reading Content-Type application/json in Spring 更改请求的内容类型以处理使用application / x-www-form-urlencoded发送的XML - Changing Content-Type of the request to process XML sent using application/x-www-form-urlencoded 使用Content-Type:application / json时,对Activiti REST API的POST请求导致CORS问题 - POST request to Activiti REST API causes CORS issue when using Content-Type:application/json 如何发送请求标题是“内容类型”:“应用程序/ json”在排球时获取 - How to send request Header is “Content-Type”:“application/json” when GET on Volley 用Java处理Content-Type =“ application / x-www-form-urlencoded”的SOAP请求 - Processing SOAP request with Content-Type = “application/x-www-form-urlencoded” in Java RESTEasy:找不到内容类型应用程序/json 类型的编写器 - RESTEasy: Could not find writer for content-type application/json type MVC-当自定义内容类型时接受JSON(不是application / json) - MVC - Accept JSON when content-type is custom (not application/json) 取消 object 从 REST 请求与 Content-Type 到 application/xml - unmarchall object from REST request with Content-Type to application/xml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM