Reputation: 333
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
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;
}
}
Upvotes: 0
Views: 1728
Reputation: 21
Laravel's VerifyCsrfToken middleware allows to specify routes, that are excluded from CSRF validation. In order to achieve that, you need to add the routes to $except array in your App\Http\Middleware\VerifyCsrfToken.php class:
Form laravel DOcs Excluding URIs From CSRF Protection Sometimes you may wish to exclude a set of URIs from CSRF protection. For example, if you are using Stripe to process payments and are utilizing their webhook system, you will need to exclude your webhook handler route from Laravel's CSRF protection.
You may exclude URIs by adding them to the $except property of the VerifyCsrfToken middleware:
Upvotes: 0
Reputation: 7420
Try this:
$request = $client->post('http://localhost:8080/api/clients/createClient',
['form_params' => json_encode(
[
'clientIp' => $clientIp,
'clientPort' => $clientPort,
'dataCollectionFrequency' => $dataCollectionFrequency,
'dataUploadFrequency' => $dataUploadFrequency,
"isClientAvailable" => "false",
'serverAllias' => $serverAllias,
'serverPort' => $serverPort,
]),
]
);
$response = $request->getBody()->getContents();
Upvotes: 1