You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Kwasi Gyasi - Agyei <kw...@4things.co.za> on 2011/09/21 19:52:34 UTC

CouchDB Inline Attachment version 1.1.0

Hi,

I recently wrote a custom couched PHP driver using CURL, I have been able to to everything except when I try to save inline attachments I'm getting the following log and response:

Headers: [{'Accept',"*/*"},
          {'Authorization',"Basic YWRtaW46YWRtaW4="},
          {'Content-Length',"137"},
          {'Content-Type',"application/json; charset=utf-8"},
          {"Expect","100-continue"},
          {'Host',"127.0.0.1:5984"}]
[debug] [<0.23325.0>] OAuth Params: []
[debug] [<0.23325.0>] Minor error in HTTP request: {doc_validation,
                                                    <<"Bad special document member: _attachments">>}
[debug] [<0.23325.0>] Stacktrace: [{io_lib_pretty,cind_tag_tuple,7},
                                   {io_lib_pretty,while_fail,3},
                                   {io_lib_pretty,print,6},
                                   {io_lib_format,build,3},
                                   {io_lib_format,build,3},
                                   {io_lib_format,build,3},
                                   {io_lib_format,build,3},
                                   {io_lib_format,build,3}]
[info] [<0.23325.0>] 127.0.0.1 - - 'PUT' /testing_client_implmentation/8a2e5f24e11ee5bf9cfc94d1400081b7 500
[debug] [<0.23325.0>] httpd 500 error response:
 {"error":"doc_validation","reason":"Bad special document member: _attachments"}

The main problem is the last line :  {"error":"doc_validation","reason":"Bad special document member: _attachments"}

not sure why couched is reject it's own field.

Please advice.

4Things
Multimedia and Communication | Property | Entertainment
Kwasi Owusu Gyasi - Agyei

cell        (+27) (0) 76 466 4488
website www.4things.co.za
email     kwasi.gyasiagyei@4things.co.za
skype    kwasi.gyasiagyei
role        Developer.Designer.Software Architect



Re: CouchDB Inline Attachment version 1.1.0

Posted by Stefan Matheis <ma...@googlemail.com>.
Kwasi,

just as a suggestion, set $this->_attachments = new stdClass() at the
constructor - otherwise you'll miss attachments .. if you're using
more then one

Regards
Stefan

On Thu, Sep 22, 2011 at 5:44 AM, Kwasi Gyasi - Agyei
<kw...@4things.co.za> wrote:
> Thanks :)
>
> All working now, included code below for whoever runs into the same blunder.
>
> (PHP) inline attachment dictionary.
>
>        /**
>         *
>         * This is used to add an inine attachment to a document.
>         *
>         * @param String $attachmentName
>         * @param String $attachmentPath
>         */
>        public function attachment( $attachmentName, $attachmentPath )
>        {
>                $attachmentContent                                 = trim( file_get_contents( $attachmentPath ) );
>                $attachmentBase64Encoded                           = base64_encode( $attachmentContent );
>                $this->_attachments                                = new stdClass();
>                $this->_attachments->$attachmentName               = new stdClass();
>                $this->_attachments->$attachmentName->content_type = "text/plain";
>                $this->_attachments->$attachmentName->data         = $attachmentBase64Encoded;
>
>        }
>
>
> 4Things
> Multimedia and Communication | Property | Entertainment
> Kwasi Owusu Gyasi - Agyei
>
> cell        (+27) (0) 76 466 4488
> website www.4things.co.za
> email     kwasi.gyasiagyei@4things.co.za
> skype    kwasi.gyasiagyei
> role        Developer.Designer.Software Architect
>
>
> On 21 Sep 2011, at 11:50 PM, Jens Alfke wrote:
>
>>
>> On Sep 21, 2011, at 1:46 PM, Kwasi Gyasi - Agyei wrote:
>>
>>> "_attachments":[
>>> {"test":
>>>     {"content_type":"text\/plain",
>>>       "data":"aGVsbG8gd29ybGQ="
>>>     }
>>> } ]
>>
>> _attachments is a dictionary, not an array. This should be:
>>
>> "_attachments”:{
>> "test":
>>     {"content_type":"text\/plain",
>>       "data":"aGVsbG8gd29ybGQ="
>>     }
>> }
>>
>> See <http://wiki.apache.org/couchdb/HTTP_Document_API#Inline_Attachments>
>>
>> —Jens
>
>

Re: CouchDB Inline Attachment version 1.1.0

Posted by Kwasi Gyasi - Agyei <kw...@4things.co.za>.
Thanks :)

All working now, included code below for whoever runs into the same blunder.

(PHP) inline attachment dictionary.

	/**
	 * 
	 * This is used to add an inine attachment to a document.
	 * 
	 * @param String $attachmentName
	 * @param String $attachmentPath
	 */
	public function attachment( $attachmentName, $attachmentPath )
	{
		$attachmentContent                                 = trim( file_get_contents( $attachmentPath ) );
		$attachmentBase64Encoded                           = base64_encode( $attachmentContent );
		$this->_attachments                                = new stdClass();
		$this->_attachments->$attachmentName               = new stdClass();
		$this->_attachments->$attachmentName->content_type = "text/plain";
		$this->_attachments->$attachmentName->data         = $attachmentBase64Encoded;
		
	}


4Things
Multimedia and Communication | Property | Entertainment
Kwasi Owusu Gyasi - Agyei

cell        (+27) (0) 76 466 4488
website www.4things.co.za
email     kwasi.gyasiagyei@4things.co.za
skype    kwasi.gyasiagyei
role        Developer.Designer.Software Architect


On 21 Sep 2011, at 11:50 PM, Jens Alfke wrote:

> 
> On Sep 21, 2011, at 1:46 PM, Kwasi Gyasi - Agyei wrote:
> 
>> "_attachments":[
>> {"test":
>>     {"content_type":"text\/plain",
>>       "data":"aGVsbG8gd29ybGQ="
>>     } 
>> } ]
> 
> _attachments is a dictionary, not an array. This should be:
> 
> "_attachments”:{
> "test":
>     {"content_type":"text\/plain",
>       "data":"aGVsbG8gd29ybGQ="
>     } 
> }
> 
> See <http://wiki.apache.org/couchdb/HTTP_Document_API#Inline_Attachments>
> 
> —Jens


Re: CouchDB Inline Attachment version 1.1.0

Posted by Jens Alfke <je...@couchbase.com>.
On Sep 21, 2011, at 1:46 PM, Kwasi Gyasi - Agyei wrote:

> "_attachments":[
>  {"test":
>      {"content_type":"text\/plain",
>        "data":"aGVsbG8gd29ybGQ="
>      } 
>  } ]

_attachments is a dictionary, not an array. This should be:

"_attachments”:{
 "test":
     {"content_type":"text\/plain",
       "data":"aGVsbG8gd29ybGQ="
     } 
 }

See <http://wiki.apache.org/couchdb/HTTP_Document_API#Inline_Attachments>

—Jens

Re: CouchDB Inline Attachment version 1.1.0

Posted by Kwasi Gyasi - Agyei <kw...@4things.co.za>.
No custom validation function, request sent appears as follows

"{"name":"Kwasi",
  "surname":"Gyasi - Agyei",
"_attachments":[
  {"test":
      {"content_type":"text\/plain",
        "data":"aGVsbG8gd29ybGQ="
      } 
  } ]

 }"                

data is base64_encoded, trimmed line breaks and carriage returns values. 

when I process the same request changing _attachments to attachments that works with no issues...

4Things
Multimedia and Communication | Property | Entertainment
Kwasi Owusu Gyasi - Agyei

cell        (+27) (0) 76 466 4488
website www.4things.co.za
email     kwasi.gyasiagyei@4things.co.za
skype    kwasi.gyasiagyei
role        Developer.Designer.Software Architect


On 21 Sep 2011, at 8:44 PM, Jens Alfke wrote:

> 
> On Sep 21, 2011, at 10:52 AM, Kwasi Gyasi - Agyei wrote:
> 
> [debug] [<0.23325.0>] Minor error in HTTP request: {doc_validation,
>                                                   <<"Bad special document member: _attachments">>}
> 
> Does the database you’re writing to have a custom validation function that’s rejecting attachments?
> 
> If it’s not that, check that the value of the _attachments property is correctly structured.
> 
> —Jens


Re: CouchDB Inline Attachment version 1.1.0

Posted by Jens Alfke <je...@couchbase.com>.
On Sep 21, 2011, at 10:52 AM, Kwasi Gyasi - Agyei wrote:

[debug] [<0.23325.0>] Minor error in HTTP request: {doc_validation,
                                                   <<"Bad special document member: _attachments">>}

Does the database you’re writing to have a custom validation function that’s rejecting attachments?

If it’s not that, check that the value of the _attachments property is correctly structured.

—Jens