Delphi firemonkey HTTP Put body 参数

iboy15

TIdHTTP.Put()在 Delphi FireMonkey 中使用编写字符串流的正确方法是什么

我有一个像这样的 PHP API (slim):

// Update user
$app->put('/api/user/update/{id}', function(Request $request, Response $response){
    $id = $request->getAttribute('id');
    $username = $request->getParam('username');
    $password = $request->getParam('password');
    $nama = $request->getParam('nama');
    $alamat = $request->getParam('alamat');
    $jenis_kelamin = $request->getParam('jenis_kelamin');
    $foto = $request->getParam('foto');

    $sql = "UPDATE user SET
        username    = :username,
        password    = :password,
                nama        = :nama,
                alamat      = :alamat,
                jenis_kelamin   = :jenis_kelamin,
                foto        = :foto
            WHERE id = $id";

    try{
        // Get DB Object
        $db = new db();
        // Connect
        $db = $db->connect();

        $stmt = $db->prepare($sql);

        $stmt->bindParam(':username', $username);
        $stmt->bindParam(':password',  $password);
        $stmt->bindParam(':nama',      $nama);
        $stmt->bindParam(':alamat',      $alamat);
        $stmt->bindParam(':jenis_kelamin',    $jenis_kelamin);
        $stmt->bindParam(':foto',       $foto);

        $stmt->execute();

        echo '{"notice": {"text": "Customer Updated"}';

    } catch(PDOException $e){
        echo '{"error": {"text": '.$e->getMessage().'}';
    }
});

Delphi 中通过 HTTP 发布数据的过程是这样的:

procedure TForm3.updateexample;
var
  lHTTP: TIdHTTP;
  lParamList: TStringList;
  stream : TStringStream;
  mydata,json: string;

begin


 json := 'username='+edtusername.Text+
          'password='+edtpassword.Text+
          'nama='+edtnama.Text+
          'alamat='+edtalamat.Text+
          'jenis_kelamin='+cbbjkel.Selected.Text+
          'foto="'+edtalamat.text;

  stream := TStringStream.Create(json,Tencoding.UTF8);

  //create
  lHTTP := TIdHTTP.Create(nil);
  try

    mydata := lHTTP.put
      ('http://myweblablalbalbala/api/user/update/'+id,stream); 

    if Pos('Customer Updated', mydata) > 0 then
    begin
      ShowMessage('Update Succes');
    end
    else
      ShowMessage('update fail');
  finally
    lHTTP.Free;
    lParamList.Free;
  end;
end;

该代码有效,但在发送更新后,数据从此更改(更新前):

{"id":"1","username":"iboy","password":"123456","nama":"ishak","alamat":"cikupa","jenis_kelamin":"l","foto":"iboy.jpg"}

对此(更新后):

{"id":"1","username":"","password":"","nama":"","alamat":"","jenis_kelamin":"","foto":""}

你能帮助我吗?通过 HTTP I Delphi 发布字符串流的正确方法是什么?

雷米勒博

您没有TStringstream正确格式化数据。它甚至不接近于 JSON(尽管你的变量名)。而且您也没有设置TIdHTTP.Request.ContentType属性,因此服务器知道您实际发送的数据类型。

如果您阅读Slim 文档,Slim 支持以下开箱即用的媒体类型:

  • 应用程序/x-www-form-urlencoded
  • 应用程序/json
  • 应用程序/xml & 文本/xml

您尝试发送的数据最接近application/x-www-form-urlencoded. 这通常是使用TIdHTTP.Post()而不是TIdHTTP.Put(), 通过给它一个成对TStrings对象来发送的name=valuePost()将为您格式化格式中的值application/x-www-form-urlencoded,并将Request.ContentType属性设置为匹配。

PHP期待的是PUT请求而不是POST请求。根据 Slim 文档,您实际上可以通过以下任POST一方式发送请求并使其表现得像PUT请求:

  • _METHOD=PUT在您的数据中包含一个值(application/x-www-form-urlencoded仅)

  • 包括一个X-HTTP-Method-Override: PUTHTTP 请求头(适用于任何媒体类型)。

尝试更像这样的事情:

procedure TForm3.UpdateExample;
var
  lHTTP: TIdHTTP;
  lParamList: TStringList;
  lReply: string;
begin
  lParamList := TStringList.Create;
  try
    lParamList.Add('username='+edtusername.Text);
    lParamList.Add('password='+edtpassword.Text);
    lParamList.Add('nama='+edtnama.Text);
    lParamList.Add('alamat='+edtalamat.Text);
    lParamList.Add('jenis_kelamin='+cbbjkel.Selected.Text);
    lParamList.Add('foto='+edtalamat.Text);

    lHTTP := TIdHTTP.Create(nil);
    try

      // use one of these, not both!
      lParamList.Add('_METHOD=PUT');
      lHTTP.Request.MethodOverride := 'PUT';

      lReply := lHTTP.Post('http://myweblablalbalbala/api/user/update/'+id, lParamList); 

      if Pos('Customer Updated', lReply) > 0 then
        ShowMessage('Update Success')
      else
        ShowMessage('Update Fail');
    finally
      lHTTP.Free;
    end;
  finally
    lParamList.Free;
  end;
end;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Delphi Firemonkey获得UTC时间

来自分类Dev

改造@Body在HTTP请求中显示为参数

来自分类Dev

Delphi从Android上的Form删除FireMonkey元素

来自分类Dev

名为Delphi 10 Firemonkey的调用方法

来自分类Dev

Delphi Firemonkey无法正确显示字体

来自分类Dev

Delphi XE6 Firemonkey表格

来自分类Dev

Delphi - Firemonkey 将文本写入 TRectangle

来自分类Dev

Delphi XE-6 Firemonkey桌面应用程序的默认Firemonkey样式表(文件)?

来自分类Dev

如何从delphi XE5 firemonkey样式提取PNG?

来自分类Dev

如何调整按钮大小以适合Delphi FireMonkey中的文本?

来自分类Dev

Delphi Firemonkey-运行时加载样式

来自分类Dev

在delphi firemonkey mobile中更改组合框的字体颜色

来自分类Dev

Delphi / Firemonkey在运行时更改iOS屏幕旋转

来自分类Dev

在delphi radstudio 10.4 firemonkey中创建新表单

来自分类Dev

在delphi radstudio 10.4 firemonkey中创建新表单

来自分类Dev

Delphi XE4 FireMonkey TMemo透明吗?(iOS)

来自分类Dev

Delphi XE6 / Firemonkey-获取实际的文本高度?

来自分类Dev

ShowVirtualKeyboard如何在Delphi FireMonkey Android平台上运行?

来自分类Dev

Delphi 10 Firemonkey-无法从adoTable拖放Tedits

来自分类Dev

Firemonkey在后台Form Delphi 10 Seattle中进行操作

来自分类Dev

delphi / firemonkey:如何在Activity中实现回调?

来自分类Dev

Delphi 10.1 Firemonkey-组件构造期间的属性值

来自分类Dev

Delphi Firemonkey 在运行时创建 TExpanders 和 TLabels

来自分类Dev

Delphi类参数

来自分类Dev

Delphi,FastReport参数

来自分类Dev

delphi idhttpserver搜索参数

来自分类Dev

Delphi Indy SSL 参数

来自分类Dev

DELPHI 实际参数不够

来自分类Dev

如何在扑http put方法中通过身体传递参数

Related 相关文章

热门标签

归档