I trying to send POST request with Form data(Laravel application) to Java Spring rest api (http://localhost:8000/api/devices/createDevice).
But i received (400 Bad Request) error.With POSTMAN everything is working perfectly but not with Guzzle.How to solve this issue ?
Images from Postman Postman Postman body piscture
Postman header picture
Error image: error
With dd: dd function
Laravel Controller
public function deviceCreate(Request $request){
$deviceIp= $request->input('deviceIp');
$devicePort= $request->input('devicePort');
$deviceFrequencyCollection= $request->input('deviceFrequencyCollection');
$deviceFrequencyUpload= $request->input('deviceFrequencyUpload');
$serverName= $request->input('serverName');
$serverAddress= $request->input('serverAddress');
$PortServer= $request->input('PortServer');
try{
$device= new Client([
'headers' => [ 'Content-Type' => 'application/json' ]
]);
$requirement= $device->post( 'http://localhost:8000/api/devices/createDevice',
['json' =>json_encode(
[
'deviceIp' => $deviceIp,
'devicePort' => $devicePort,
'deviceFrequencyCollection' => $deviceFrequencyCollection,
'deviceFrequencyUpload' => $deviceFrequencyUpload,
"deviceStatus" => "false",
'serverName' => $serverName,
'PortServer' => $PortServer
] )
]
);
$answer= $device->send($requirement);
} catch (RequestException $e) {
dd($e->getRequest());
}
return redirect()->route('graph.home')->with('info', 'Success ');
}
}``
Client Controller.java(spring controller)
/* 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(model spring)
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;
}
}