Skip to content

Question: Invalid value provided (invalid IRI?) Error testing POST endpoint #1099

@msechi

Description

@msechi

I have a Product entity (that implements an interface I made - AuthoredUsedEntity), related to another User entity with a relation manyToOne. AuthoredUsedEntity is used to populate the user field in Product with the current logged in User.

when I make an api call to /api/products (I use postman) passing a json like this:

{
"name": "my product",
"price": 10
}

this works and a new record is created with the correct userID. The problem is with unit tests. I make a call like this:

$headers = [
    'HTTP_AUTHORIZATION' => 'Bearer ' . $token,
    'CONTENT_TYPE' => 'application/ld+json',
    'HTTP_ACCEPT' => 'application/ld+json'
];
$postData = [
'name' => 'my product',
'price' => 10
];
$client = $this->makeClient();
$client->request('POST', '/api/products', [], [], $headers, json_encode($postData));

and I get this error: Invalid value provided (invalid IRI?)

am I doing anything wrong? For tests I am using phpunit and Liip.

thanks
M

Activity

msechi

msechi commented on Apr 12, 2019

@msechi
Author

any idea anyone?

Arkowsky

Arkowsky commented on Apr 15, 2019

@Arkowsky

Try use just application/json as CONTENT_TYPE and HTTP_ACCEPT in your tests in this case.

msechi

msechi commented on Apr 15, 2019

@msechi
Author

@Arkowsky yes I tried already, same result.
thanks

Arkowsky

Arkowsky commented on Apr 16, 2019

@Arkowsky

I think you have to setId after you create object and this is the issue. Your identifier is empty and after create is called normalizer to show your results so you need to have id set.

msechi

msechi commented on Apr 16, 2019

@msechi
Author

@Arkowsky as mentioned it works using POSTMAN. The issue must be something different given that it is failing when testing only, and only with POST/PUT calls.

coolfarmer

coolfarmer commented on Jan 2, 2020

@coolfarmer

@Arkowsky as mentioned it works using POSTMAN. The issue must be something different given that it is failing when testing only, and only with POST/PUT calls.

Same problem here, have you found a solution?

jonytrifulca

jonytrifulca commented on May 13, 2020

@jonytrifulca

Take a look on this line

$client->request('POST', '/api/products', [], [], $headers, json_encode($postData));

try to don´t make a json_encode() , only pass the $postData like an array.

$response = $client->request( 'POST', $url, [ 'headers' => [ 'Content-Type' => 'application/json', 'Accept' => 'application/json' ], 'json' => $postData ]);

silviodeligios

silviodeligios commented on Apr 28, 2022

@silviodeligios

I have the same problem :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @teohhanhui@coolfarmer@jonytrifulca@silviodeligios@Arkowsky

        Issue actions

          Question: Invalid value provided (invalid IRI?) Error testing POST endpoint · Issue #1099 · api-platform/api-platform