http_read_data(Request, Data, []) post prolog 数据

http_read_data(Request, Data, []) post data to prolog

我正在尝试制作序言 api,我从另一个程序调用给定的序言 link。经过几次尝试和学习 prolog,这是我能写的最好的代码版本

#!/usr/bin/env swipl
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_client)).
:- initialization main.
:- http_handler(/, index, []).
:- http_handler('/test', test, []).
:- http_handler('/reply', reply, []).

index(_Request) :-
      format('Content-type: text/plain~n~n'),
      format('Hello World!~n').

test(_Request) :-
      format('Content-type: text/plain~n~n'),
      format('This is another test, trying to make multiple links!~n').

reply(Request) :-
      member(method(post), Request), !,
      http_read_data(Request, Data, []),
      format('Content-type: text/plain~n~n'),
      format('This is where the data should be displayed!~n').
      /*print(Data).*/
      

main :-
  http_server(http_dispatch, [port(3000)]),
  thread_get_message(quit).

回复的主要功能给我一个内部服务器错误和语法错误:illegal_uri_query 当我省略这一行时: http_read_data(请求,数据,[]), 一切正常,所以我相信有一些我无法弄清楚的事情。

注意:我发送的数据只是一个字符串,通过下面的代码写在laravel:

$ch = curl_init('http://localhost:3000/reply');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);

// execute!
$response = curl_exec($ch);

// close the connection, release resources used
curl_close($ch);

此代码适用于其他两个页面索引和测试。

确保通过添加 content-type

来表明您发送的是 JSON 数据
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));