Laravel guzzle post请求创建功能不起作用(415 Unsupported Media Type`)

问题描述 投票:1回答:1

我尝试将带有表单数据(Laravel应用程序)的POST请求发送到Java Spring rest api(http://localhost:8000/api/devices/createClient)。

但是我收到了(415 Unsupported Media Type`)错误。有了POSTMAN,一切都运行得很好,但没有Guzzle.How如何解决这个问题?

图片来自邮递员Postman body piscture Postman 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;
    }

}
spring laravel rest guzzle guzzle6
1个回答
0
投票

这就是我在评论中提到的内容

$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

然后...:

$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:了解我所指的是什么。

© www.soinside.com 2019 - 2024. All rights reserved.