1
votes

I trying to send POST request with Form data(Laravel application) to Java Spring rest api (http://localhost:8000/api/devices/createClient).

But i received (415 Unsupported Media Type`) error.With POSTMAN everything is working perfectly but not with Guzzle.How to solve this issue ?

Images from Postman Postman body piscture Postman header picture

Error image: Laravel error

Laravel Controller

 //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(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;
    }

}
1
The error seems to be a response to your request. You may want to use 'json' instead of 'form_params' in your guzzle request. check: docs.guzzlephp.org/en/stable/request-options.html#jsonOluwatobi Samuel Omisakin
@OluwatobiSamuelOmisakin, Please see tjos question : stackoverflow.com/questions/51064433/…Nenad
you don't need json_encode() if you use json you only need to pass an array of that request body.Oluwatobi Samuel Omisakin

1 Answers

0
votes

This is what I am referring to in my comment

$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

enter image description here

Then...:

$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'),
]]);

Be aware that some behaviors are different with Guzzle version: Check the answers here: to understand what I'm referring to.