使用Guzzle发送发送http请求时出现错误500内部服务器错误

时间:2017-12-02 13:36:31

标签: php http http-post guzzle http-status-code-500

我正在尝试使用Guzzle发送一个帖子http请求,但我收到错误500

            }

            //Distance
            double dst = Math.Abs(((p.X - e.X) + (p.Y - e.Y))); 

            //Angle
            double angle = Math.Atan2(e.Y - p.Y, e.X - p.X);
            angle = Math.Round((angle * 180) / Math.PI, 0);

            Label label = new Label();
            this.panel1.Controls.Add(label);
            label.Location = e.Location;
            label.Text = dst.ToString() + "@" + angle.ToString() + "°";
            List<Label> Labels = new List<Label>();
            Labels.Add(label);

            if (Labels.Count > 0)
            {
                this.panel1.Controls.RemoveAt(Labels.Count-1);

            }
            panel1.Invalidate();
        }

    }
private void panel1_MouseDown(object sender, MouseEventArgs e)// On mouse down
    {
        Graphics g = Graphics.FromImage(Bitmap);
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        if (dist == false && circ == false) //Distance Measure and Circle Drawing not initiated
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                leftdown = true;
                count++;
                Dot dot = new Dot(e.X, e.Y, 2, 2, Color.White,e.Location);
                Dots.Add(dot);
                dot.DrawPoint(g);
                panel1.Invalidate();
                PointsList.Add(new System.Drawing.Point(e.X, e.Y)); //Add points on each click
                PointF[] pts = new PointF[] { new PointF(e.X, e.Y) };
                gp.AddLines(pts);
                if (PointsList.Count > 1)
                {
                    //  DrawLineP(PointsList[PointsList.Count - 2].X, PointsList[PointsList.Count - 2].Y, e.X, e.Y);

                  //  Line line = new Line(Color.White, 2, new Point(PointsList[PointsList.Count - 2].X, PointsList[PointsList.Count - 2].Y), new Point(e.X, e.Y));
                   // Lines.Add(line);

                }

            }
            else if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                foreach (Line line in Lines)
                {
                    line.color = line.SelectLine(e.Location) ? Color.Red : Color.White;

                    panel1.Invalidate();
                }

            }
        }
        else if (dist == true && circ == false)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                DsP1 = new Point(e.X, e.Y); //First point of Distance Measure
                g.FillRectangle(Brushes.Yellow, new Rectangle(DsP1, new Size(1, 1)));
                label9.Font = font4;
                label10.Font = font3;
                g.DrawRectangle(RPen, new System.Drawing.Rectangle(e.X, e.Y, 1, 1));
            }
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                contextMenuStrip1.Show(e.Location);

                DsP2 = new Point(e.X, e.Y); //Second Point of Distance Measure
                g.FillRectangle(Brushes.Yellow, new Rectangle(DsP2, new Size(1, 1)));

                double x = DsP1.X - DsP2.X;
                double y = DsP1.Y - DsP2.Y;
                double d = Math.Abs(x + y);
                float midX = (DsP1.X + DsP2.X) / 2;
                float midY = (DsP1.Y + DsP2.Y) / 2;
                DrawDist(DsP1, DsP2, d, midX, midY, Color.White); //Method
            }

        }
        if (circ == true && dist == false) //Circle Drawing
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {

                PointsList.Add(new System.Drawing.Point(e.X, e.Y)); //Add points on each click

                if (PointsList.Count > 1)
                {
                    DsP1 = PointsList[0];
                    DsP2 = PointsList[1];
                    // DrawCirc(DsP1, DsP2, Color.White); //Method
                    Circle circle = new Circle(Color.White, 2, DsP1, (DsP2.Y - DsP1.Y));
                    Circles.Add(circle);
                    PointsList.Clear();
                    circ = false;
                    panel1.Invalidate();
                }


            }

            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                foreach (Circle circle in Circles)
                {
                    circle.color= circle.SelectCircle(e.Location) ? Color.Red : Color.White;
                    panel1.Invalidate();
                }

            }

        }


        //Angle Measurement
        if (angle_select == true || fillet==true)
        {
            if (count == 1)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    foreach (Line line in Lines)
                    {
                        if (line.color == Color.Red)
                        {
                            AngL1P1 = line.Start;
                            AngL1P2 = line.End;
                            count++;

                        }
                    }
                }
            } 
            else if (count == 2)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    foreach (Line line in Lines)
                    {
                        if (line.color == Color.Red)
                        {
                            AngL2P1 = line.Start;
                            AngL2P2 = line.End;
                            count = 0;
                            angle_select = false;
                            AngleMeasure(g);
                            panel1.Invalidate();
                            panel1.Cursor = this.Cursor;
                        }
                    }
                }
            }

        }

    }
private void panel1_Paint(object sender, PaintEventArgs e)
    {
        Graphics grp = e.Graphics;

        grp.SmoothingMode = SmoothingMode.AntiAlias;
        grp.DrawImage(Bitmap, panel1.Location.X, panel1.Location.Y - 158);

        DrawPolygon(grp);
        DrawAxes();
        DrawGrid();
        foreach (Line L in Lines.Except(DelLines))
        {
            L.Draw(grp);
        }
        foreach(Circle C in Circles)
        {
            C.Draw(grp);
        }
        foreach(Dot p in Dots)
        {
            p.DrawPoint(grp);
        }
        grp.Dispose();
    }

我尝试使用Postman发送相同的标题和正文密钥,它没有任何问题:

Internal server error 
ServerException in RequestException.php line 113:
Server error: `POST https://domainname/api/v1/user/register` resulted in a `500 Internal Server Error` response:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="robots" content="noindex,nofo (truncated...)

部首: enter image description here 身体: enter image description here

这是我的Guzzle客户请求: -

{
    "status": "true"
}

我去那个api的路线: -

        $client = new GuzzleHttp\Client(['base_uri' => 'https://domainname/api/v1/']);
                   $response = $client->request('POST', 'user/register', [
                       'headers' => [
                           'Authorization' => Session::get('token'),
                           'Content-Type'     => 'application/x-www-form-urlencoded',
                       ],
                       'form_params' => [
                           'start_date' => $start_date,
                           'expiry_date' => $expiry_date,
                           'type' => 'normal',
                           'status' => 'active',

                       ]
                   ]);

                    $response= $response->getBody();
                    $re= json_decode($response);

我的注册控制器:

Route::post('/user/register', 'UsersAPI@register'); 

NGINX error.log: -

public function register(Request $request){
Log::info('Request: ' . $request);
//dd($request->all());
     $validator = Validator::make($request->all(), [
        // 'user_id' => 'required',
        'start_date' => 'required',
        'expiry_date' => 'required',
        'type' => 'required',
        'status'=>'required',   
        //'receipt' => 'required',
        //'purchase_id'=>'required',
    ]);

    $user = JWTAuth::toUser(); 
      //Check whether validation is failed or passed
    if($validator->fails()){
        //Redirect back with validation errors
        return response()->json(['error' => $validator->errors()], 400);
    }



         $user = new User($data);
        $user->user()->save($user);
     Log::info('save_user done for this user: ' . $user->id);


    return ['status' => 'true']; 
}

1 个答案:

答案 0 :(得分:0)

如果相同的代码根据您发送请求的方式产生不同的结果,我会假设请求有效负载之间仍然存在差异。

您是否仔细检查了Session :: get('token'),$ start_date和$ expiry_date的内容?