【问题标题】:Laravel guzzle post request for create function not working(400 Bad Request)Laravel guzzle post 请求创建功能不起作用(400 错误请求)
【发布时间】:2018-12-06 10:43:36
【问题描述】:

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

但我收到(400 Bad Request)错误。使用 POSTMAN 时一切正常,但使用 Guzzle 时没有。如何解决此问题?

图片来自邮递员邮递员Postman body piscture

Postman header picture

错误图片:error

带dd:dd function

Laravel 控制器

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

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

    }

【问题讨论】:

  • 在 guzzle 请求中将 json 替换为 body
  • 再次出现同样的错误(400) :(

标签: laravel rest guzzle


【解决方案1】:

试试这个:

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

【讨论】:

  • 非常感谢您的回答,但现在它说我有“500 内部服务器错误”
  • @Nenad 尝试将body 更改为form_params
  • 现在是400 Bad Request 回复:ohhhhhhhhhhhh 我不知道有没有可能:(
  • @Nenad 将其改回 500 错误,并将参数 isClientAvailable 更改为 false 而不是字符串 '"false"'
  • 再次没有,总是 400 错误,我尝试了所有 (json,body,form_params)
【解决方案2】:

Laravel 的 VerifyCsrfToken 中间件允许指定从 CSRF 验证中排除的路由。为了实现这一点,您需要在 App\Http\Middleware\VerifyCsrfToken.php 类中将路由添加到 $except 数组:

形成 laravel 文档 从 CSRF 保护中排除 URI 有时您可能希望从 CSRF 保护中排除一组 URI。例如,如果您使用 Stripe 处理付款并使用他们的 webhook 系统,则需要从 Laravel 的 CSRF 保护中排除您的 webhook 处理程序路由。

您可以通过将 URI 添加到 VerifyCsrfToken 中间件的 $except 属性来排除它们:

【讨论】:

    猜你喜欢
    • 2018-12-06
    • 2017-04-10
    • 2016-11-18
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多