postman 因为当发送base64文件时,它会给我一个414错误

xyhw6mcr  于 2022-11-07  发布在  Postman
关注(0)|答案(1)|浏览(456)

我正在通过POST发送base64文件pdf,但它给我一个错误,这是太长了
代码控制器输入数据,验证输入的数据类型,如果是base64,则分配并发送数据,如果是html,则将其转换为pdf,然后转换为base64,并以json格式返回响应

public function subirResolucion(Request $req){
    $auth = new CeroPapelAuthenticator();
    $auth->setUri(getenv("AUTH_URI"));
    $auth->setSistema(getenv("AUTH_SYSTEM"));
    $auth->setUser(getenv("AUTH_USER"));
    $auth->setPassword(getenv("AUTH_PASS"));

    $response = $auth->access();
    $auth->setToken($response["token"]);
    $auth->setCookie($response["cookie"]);

    $connector = new GestorDocumentalFactory();
    $gd = $connector->crearCeroPapel();

    $id_documento = $req->input("id_documento");
    $transparencia = $req->input("transparencia");
    $base64 = $req->input("base64"); 
    $html = $req->input("html");

    if(!isset($base64))
    {

    $mpdf = new \Mpdf\Mpdf();
    $mpdf->WriteHTML($html);

    $pdfAbase64= base64_encode($mpdf->Output());
    $gd->setArchivo($pdfAbase64);
    }else{
    $gd->setArchivo($base64);
    }

    $gd->setIdDocumento($id_documento);
    $gd->setTransparencia($transparencia);
    $gd->setSistema("erp");
    $gd->setAuth($auth);
    $gd- 
    >setUri(getenv("SERVICE_URI")."/api/indap/ws2_obtener.php");
     try {
         $subirResolucion = $gd->subirResolucion();
         return response()->json(
              $subirResolucion 
         );
     } catch (\Exception $e) {
         return response()->json([
             "status" => "401",
             "mensaje" => "Ocurrió un error al enviar los 
     mensajes 
     ".$e->getMessage()
         ]);
     }
     }

通过POST将数据发送到另一个系统的型号

public function subirResolucion()
{

    $filter = [
        'sistema' => $this->getSistema(),
        'id_documento' => $this->getIdDocumento(),
        'transparencia' => $this->getTransparencia(),
        'archivo' => $this->getArchivo(),  
    ];

    $url = $this->getUri() . '?data=' . (string) 
    json_encode($filter);
    SysLogger::info("Request URI", print_r($url, true));

    $header = [ 
        "Content-Type: application/json",
        "Authorization: Token ".$this->auth->getToken(),
        "Cookie: ".$this->auth->getCookie()
    ];
    SysLogger::info("Headers to be sent", print_r($header, 
    true));

    $response = Page::post($url, [], '', $header);
     print_r($response);
    SysLogger::info("Full response", print_r($response, true));

    $body = json_decode($response['body']);
   // print_r($body);
    SysLogger::info("Body", print_r($body, true));

    return $body;

}

这是一个page.php类
如果有人可以帮我发送数据,它仍然会给我一个错误,希望这段代码可以帮助

class Page
 {
/**
 * get, obtiene pagina web por metodo get
 * @param  string   $url    URL a la que se hace referencia
 * @param  array    $header Arreglo de cabeceras
 * @param  integer  $timeout Segundos de espera a la pagina
 * @return array    ('body', 'head' 'cookie')
 */
static public function get($url, $header=array(), $timeout=10)
{
    $curl_handle=curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, $url);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($curl_handle, CURLINFO_HEADER_OUT, true);
    @curl_setopt($curl_handle, CURLOPT_COOKIELIST, true);
    if (count($header)>0) {
        curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $header);
    }
    $body=curl_exec($curl_handle);
    $error=curl_error($curl_handle);
    $head=curl_getinfo($curl_handle, CURLINFO_HEADER_OUT);
    $cookie=@curl_getinfo($curl_handle, CURLINFO_COOKIELIST);
    $str_cookie=self::cookieToString($cookie);
    curl_close($curl_handle);
    return array('body'=>$body, 'head'=>$head, 
  'cookie'=>$str_cookie, 'error'=>$error);
  }

/**
 * post, obtiene pagina web por metodo post
 * @param  string   $url    URL a la que se hace referencia
 * @param  string   $param  string con la lista de parametros
 * @param  string   $cookie cookie
 * @param  array    $header Arreglo de cabeceras
 * @param  integer  $timeout Segundos de espera a la pagina
 * @return array    ('body', 'head' 'cookie')
 */
static public function post($url, $param, $cookie='', 
$header=array(), $timeout=10)
   {
    $curl_handle=curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, $url);
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($curl_handle, CURLINFO_HEADER_OUT, true);
    @curl_setopt($curl_handle, CURLOPT_COOKIELIST, true);
    curl_setopt($curl_handle, CURLOPT_POST, true);
    curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $param);
    if ($cookie!='') {
        curl_setopt ($curl_handle, CURLOPT_COOKIE, $cookie);
    }
    if (count($header)>0) {
        curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $header);
    }
    $body=curl_exec($curl_handle);
    $error=curl_error($curl_handle);
    $head=curl_getinfo($curl_handle, CURLINFO_HEADER_OUT);
    $cookie=@curl_getinfo($curl_handle, CURLINFO_COOKIELIST);
    $status_code=curl_getinfo($curl_handle, 
     CURLINFO_RESPONSE_CODE);
    $str_cookie=self::cookieToString($cookie);
    curl_close($curl_handle);
    return array('body'=>$body, 'head'=>$head, 
  'cookie'=>$str_cookie, 'error'=>$error, 'status' => 
   $status_code);
    }
lb3vh1jj

lb3vh1jj1#

正如我在评论中提到的,你是在URL中发送数据,而不是通过POST请求体发送数据。

$filter = [
    'sistema' => $this->getSistema(),
    'id_documento' => $this->getIdDocumento(),
    'transparencia' => $this->getTransparencia(),
    'archivo' => $this->getArchivo(),  
];

$url = $this->getUri() . '?data=' . (string) json_encode($filter);

我不确定您使用哪个库来发送请求,但我认为这里的第二个空数组可能是您需要发布post请求的主体数据的位置。

Page::post($url, [**Data in here**], '', $header);

**编辑

我不知道Page对象是做什么的,所以在你当前的实现中很难说。但是如果我在做的话,我会使用HTTP客户端中内置的Laravels。

$filter = [
    'sistema' => $this->getSistema(),
    'id_documento' => $this->getIdDocumento(),
    'transparencia' => $this->getTransparencia(),
    'archivo' => $this->getArchivo(),  
];

$response = Http::withHeaders([ 
    "Content-Type: application/json",
    "Authorization: Token ".$this->auth->getToken(),
    "Cookie: ".$this->auth->getCookie()
])->post($this->getUri(), [
    'data' => (string) json_encode($filter),
]);

**编辑2

正如我在最初的答案中提到的,第二个变量是post主体的参数,而不是响应的参数,这是一种常见的方法,尽管看起来您必须将其设置为字符串,所以我认为您需要

Page::post($url, (string) json_encode($filter), '', $header);

这是文件中的帮助


* @param  string   $param  string con la lista de parametros

相关问题