Accessing Laravel Rest API with curl - JSON

Borjante

I'm trying to get my client to work with my API but I'm having some trouble, i'm new to cURL so maybe you can help me.

My API controller (laravel.project/api/v1/users/{id}

public function show($user)
    {

       if ($user == null)
            return $this->setStatusCode(404)->respondNotFound('User not found');

        return $this->respond($user);
    }

This is the answer I get by making a simple request with POSTMAN

{
    "data": {
        "id": 4,
        "name": "Helena",
        "email": "[email protected]",
        "created_at": "2015-03-26 21:13:16",
        "updated_at": "2015-03-26 21:13:16"
    }
}


Response Headers:

Cache-Control → no-cache
Connection → keep-alive
Content-Type → application/json
Date → Sat, 28 Mar 2015 15:44:45 GMT
Server → nginx/1.6.2
Transfer-Encoding → chunked

Everything fine till now, but then, when I use cURL in my client this way:

public function delete($url, $token)
    {

        $final_url = $this->base_url . $url;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $final_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer ' . $token, 'Content-Type: application/json'));

        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }

The response I get is not a json:

"{
  "data":{
    "id":4,
    "name":"Helena",
    "email":"[email protected]",
    "created_at":"2015-03-26 21:13:16",
    "updated_at":"2015-03-26 21:13:16"}
}"

It looks like a json response but notice the quotes at the begging and at the end, I can't parse it neither with json_encode or json_decode.

I've been struggling with this for a while any opinion are apreciated.

Just to make sure you understand my problem, code is working well, but when making the request with curl it doesn't use the content-type header

EDIT 1:

{#165
  +"data": {#167
    +"id": 4
    +"name": "Helena"
    +"email": "[email protected]"
    +"created_at": "2015-03-26 21:13:16"
    +"updated_at": "2015-03-26 21:13:16"
  }
}

Michael Bordash

Instead of return $result, try:

return response()->json(json_decode($result));

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Accessing a json api with php

分類Dev

PHP cURL JSON API

分類Dev

Best Practice for accessing REST API with plaintext password

分類Dev

Laravel 5 REST Api

分類Dev

convert curl get to rest api to python

分類Dev

Configuring Keycloak through its REST API with cUrl

分類Dev

LaravelからAPIへのJSONを使用したCURL

分類Dev

Issue in accessing API/json with multiple sections

分類Dev

Accessing Json data through api with Python

分類Dev

Angular 2 accessing REST API error -No provider for Http

分類Dev

flutter rest api get json

分類Dev

Curlを使用したAzureStorage Table API REST

分類Dev

Laravel:curlとpostmanを使用した複数の画像のアップロードの問題(REST apiで)

分類Dev

Laravel Rest API Google Auth at Angular

分類Dev

Laravel REST API request object is empty

分類Dev

Packages/libraries to build a Laravel 5 REST API

分類Dev

Accessing nested values in JSON data from Twitch API

分類Dev

REST API on Python for JSON type data

分類Dev

How to parse JSON response from Rest API

分類Dev

Spring Boot accessing MongoDB - multiple records/documents update from rest api endpoint

分類Dev

Laravel accessing hasMany of hasOne

分類Dev

Laravel Eloquent Accessing Original

分類Dev

Accessing an API that downloads a file

分類Dev

How to create a resource mapping node in AEM using sling REST api and curl

分類Dev

PHP、cURL、REST_API、JSON-メソッド「PUT」を使用してデータフィールドに値を挿入します

分類Dev

Laravel 4 REST API 結合テーブル

分類Dev

JSONでSteam Rest APIを使用する

分類Dev

RDBMSへのREST API JSONの保存

分類Dev

Display a Hashmap json from REST API, as a table in Angular 5

Related 関連記事

ホットタグ

アーカイブ