Laravel guzzle post 请求创建功能不起作用(415 Unsupported Media Type`)
Posted
技术标签:
【中文标题】Laravel guzzle post 请求创建功能不起作用(415 Unsupported Media Type`)【英文标题】:Laravel guzzle post request for create function not working(415 Unsupported Media Type`) 【发布时间】:2018-12-06 05:42:01 【问题描述】:我尝试将带有表单数据(Laravel 应用程序)的 POST 请求发送到 Java Spring rest api (http://localhost:8000/api/devices/createClient)。
但我收到 (415 Unsupported Media Type`) 错误。使用 POSTMAN 时一切正常,但使用 Guzzle 时不行。如何解决此问题?
来自邮递员的图片 Postman body piscturePostman header picture
错误图片: Laravel error
Laravel 控制器
//function for sending data to restapi
public function posDeviceCreate(Request $request)
$device= new Client([
'headers' => ['Content-Type' => 'application/json' ]
]);
$answer= $device->POST('http://localhost:8000/api/devices/createDevice',
['form_params' => json_encode(
[
[
'deviceIp' => $request->input('deviceIp'),
'devicePort' => $request->input('devicePort'),
'deviceFrequencyCollection' => $request->input('deviceFrequencyCollection'),
'deviceFrequencyUpload' => $request->input('deviceFrequencyUpload'),
"deviceStatus"=> "false",
'serverName' => $request->input('serverName'),
'serverAddress' => $request->input('serverAddress'),
'PortServer' => $request->input('PortServer')
]
]
)]
);
return redirect()->route('graph.home')->with('info', 'Success ');
Client Controller.java(弹簧控制器)
/* For client registration at the point of the client installation */
@RequestMapping(value = "/clients/createClient", method = RequestMethod.POST)
public ResponseEntity<?> createClient(@RequestBody ClientDto clientDto)
Long id = clientService.createClient(clientDto);
return new ResponseEntity<Long>(id, HttpStatus.OK);
Client.java(模型弹簧)
public class Client
@Id
@GeneratedValue
@Column(name = "client_id")
private Long clientId;
@NotNull
@Column(unique = true)
private String clientAllias;
@NotNull
private String clientIp;
@NotNull
private String clientPort;
@NotNull
private Double dataCollectionFrequency;
@NotNull
private Double dataUploadFrequency;
@NotNull
private Boolean isClientAvailable;
@NotNull
private String serverAllias;
@NotNull
private String serverIp;
@NotNull
private String serverPort;
@OneToMany
@JoinColumn(name = "client_id")
private List<ClientData> clientData = new ArrayList<ClientData>();
public Client()
public Client(ClientDto clientDto)
super();
this.clientAllias = clientDto.getClientAllias();
this.clientIp = clientDto.getClientIp();
this.clientPort = clientDto.getClientPort();
this.dataCollectionFrequency = clientDto.getDataCollectionFrequency();
this.dataUploadFrequency = clientDto.getDataUploadFrequency();
this.isClientAvailable = clientDto.getIsClientAvailable();
this.serverAllias = clientDto.getServerAllias();
this.serverIp = clientDto.getServerIp();
this.serverPort = clientDto.getServerPort();
public Long getClientId()
return clientId;
public void setClientId(Long clientId)
this.clientId = clientId;
public String getClientAllias()
return clientAllias;
public void setClientAllias(String clientAllias)
this.clientAllias = clientAllias;
public String getClientIp()
return clientIp;
public void setClientIp(String clientIp)
this.clientIp = clientIp;
public String getClientPort()
return clientPort;
public void setClientPort(String clientPort)
this.clientPort = clientPort;
public Double getDataCollectionFrequency()
return dataCollectionFrequency;
public void setDataCollectionFrequency(Double dataCollectionFrequency)
this.dataCollectionFrequency = dataCollectionFrequency;
public Double getDataUploadFrequency()
return dataUploadFrequency;
public void setDataUploadFrequency(Double dataUploadFrequency)
this.dataUploadFrequency = dataUploadFrequency;
public Boolean getIsClientAvailable()
return isClientAvailable;
public void setIsClientAvailable(Boolean isClientAvailable)
this.isClientAvailable = isClientAvailable;
public String getServerAllias()
return serverAllias;
public void setServerAllias(String serverAllias)
this.serverAllias = serverAllias;
public String getServerIp()
return serverIp;
public void setServerIp(String serverIp)
this.serverIp = serverIp;
public String getServerPort()
return serverPort;
public void setServerPort(String serverPort)
this.serverPort = serverPort;
public List<ClientData> getClientData()
return clientData;
public void setClientData(List<ClientData> clientData)
this.clientData = clientData;
【问题讨论】:
该错误似乎是对您的请求的响应。您可能希望在您的 guzzle 请求中使用“json”而不是“form_params”。检查:docs.guzzlephp.org/en/stable/request-options.html#json @OluwatobiSamuelOmisakin,请参阅 tjos 问题:***.com/questions/51064433/… 如果您使用json
,则不需要json_encode()
,您只需要传递该请求正文的数组即可。
【参考方案1】:
这就是我在评论中所指的内容
$client = new Client(); //infact you don't need the header, this'd be set for you by guzzle if you specify 'json' as the body
// Check: http://docs.guzzlephp.org/en/stable/request-options.html#json
然后……:
$client->post('/url', [
'json' => [
'clientIp' => $request->input('clientIp'),
'clientPort' => $request->input('clientPort'),
'dataCollectionFrequency' => $request->input('dataCollectionFrequency'),
'dataUploadFrequency' => $request->input('dataUploadFrequency'),
'isClientAvailable'=> 'false',
'serverAllias' => $request->input('serverAllias'),
'serverIp' => $request->input('serverIp'),
]]);
请注意,Guzzle 版本的某些行为有所不同:查看答案here:以了解我所指的内容。
【讨论】:
首先,我要感谢您为我提供的所有帮助。我解决了这个问题 :) 半天后我意识到我将我的数据发送到错误的 url :D :) 再次感谢你拯救了我的一天!以上是关于Laravel guzzle post 请求创建功能不起作用(415 Unsupported Media Type`)的主要内容,如果未能解决你的问题,请参考以下文章
POST 请求适用于 Postman,但不适用于 Guzzle
使用 Guzzle 将数据和文件一起发布到 Laravel API
Guzzle - Laravel。如何使用 x-www-form-url-encoded 发出请求
带有 Laravel 和 Guzzle 的 Matrix RERSful API。请求创建