Laravel Guzzle创建功能的请求不起作用(400错误的请求) [英] Laravel guzzle post request for create function not working(400 Bad Request)

查看:74
本文介绍了Laravel Guzzle创建功能的请求不起作用(400错误的请求)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但是我收到了(400错误请求)错误.使用POSTMAN可以正常运行,但不能使用Guzzle.如何解决此问题?

But i received (400 Bad Request) error.With POSTMAN everything is working perfectly but not with Guzzle.How to solve this issue ?

邮递员邮递员的图像邮递员尸体

邮递员标题图片

错误图片:错误

使用dd: dd函数

Laravel控制器

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(弹簧控制器)

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(模型弹簧)

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

    }

推荐答案

尝试一下:

$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();

这篇关于Laravel Guzzle创建功能的请求不起作用(400错误的请求)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆